
 

let menu=[
  {xingming:'张三',bumen:'维修一部',work:'电工',level:'二级'},
  {xingming:'李四',bumen:'维修二部',work:'机修',level:'一级'},
  {xingming:'李斯',bumen:'维修三部',work:'电工',level:'一级'},
  {xingming:'阿迪',bumen:'维修二部',work:'机修',level:'二级'},
  {xingming:'尼克',bumen:'维修一部',work:'电工',level:'一级'},
  {xingming:'铁牛',bumen:'维修二部',work:'机修',level:'二级'},
]
let newDate=menu
function showMenu(data){
 $('#tbody').html('')
  $.each(data,(index,item)=>{
    $('#tbody').append(`
    <tr>
    <td class="col-md-2 item">${item.xingming}</td> 
    <td class="col-md-2 item">${item.bumen}</td> 
    <td class="col-md-2 item">${item.work}</td> 
    <td class="col-md-2 item">${item.level}</td> 
    <td class="col-md-2 item">
    <button class="glyphicon glyphicon-pencil tianjia" aria-hidden="true"  data-item='${JSON.stringify(item)}'></button>
    <button class="glyphicon glyphicon-trash shanchu" aria-hidden="true"  data-item='${JSON.stringify(item)}'></button>
    </td> 
    </tr>
    `)
})
}
showMenu(newDate)
 //重置
 $('#chongzhi').click(function () {
  showMenu(menu)
})
//查询
$('#chaxun').click(function (){
  let data1=$('#input_1').val()
  let data2=$('#bumen').val()
  let data3=$('#work').val()
  newDate=[]
  for (let i = 0; i <menu.length; i++) {
  if(menu[i].xingming==data1){
    newDate.push(menu[i])
    showMenu(newDate)
  }else if(menu[i].bumen==data2){}
  else if(menu[i].state==data3){}
  }
 })


 
 $('.tianjias').click(function (){
   window.location.href="tianjiarenyuan.html"  
   setData('kby',menu)
 })
 $('#tbody').on('click','.tianjia',function(){
  window.location.href="tianjiarenyuan.html"  
  setData('kby',menu)
 })

 // 上传到本地存储的方法
function setData(key,value){
 // key:用来放value的一个空间 value:这里用来存储了一个JSON对象
 // 将JSON对象转换成字符串
 let valueStr = JSON.stringify(value)
 // 将JSON格式的字符串上传到浏览器本地存储
 sessionStorage.setItem(key,valueStr)

}

function getData(key){
  let newData=JSON.parse(sessionStorage.getItem(key))
  if(newData != null){
    menu = newData
  }
 showMenu(menu)
}
getData('kby')
//删除
$('#tbody').on("click", ".shanchu",function (){
  let obj1 = JSON.parse($(this).attr('data-item'))
  for(let i=0;i<menu.length;i++){
    if (obj1.xingming==menu[i].xingming) {
      menu.splice(i,1)
      showMenu(menu)
    }
  }
})
 //抽组
 $('#chouzu').click(function (){
window.location.href="renyuanchouzu.html"
setData('kby',menu)
 })